package com.hhs.storage.controller;


import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.hhs.storage.bean.*;
import com.hhs.storage.service.YymCjdataService;
import com.hhs.storage.service.YymUserCjLinkService;
import com.hhs.storage.util.ExcelExport2;
import com.hhs.storage.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yuanjun
 * @since 2020-08-13
 */
@RestController
@RequestMapping("/app/yymcjdata")
@Slf4j
public class AppYymCjdataController {
    @Resource
    private YymCjdataService yymCjdataService;
    @Resource
    private YymUserCjLinkService yymUserCjLinkService;

    @RequestMapping(value = "getPage")
    public Map<String, Object> getPage(InputParam inputParam) {
        log.info("查询用户分页信息");
        Page<YymCjdata> result = yymCjdataService.findPage(new Page<YymCjdata>(inputParam.getPage(), inputParam.getSize()),inputParam);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("rows", result.getRecords());
        map.put("total",result.getTotal());

        return map;
    }

    @RequestMapping(value = "getAll")
    public Map<String, Object> getAll(InputParam inputParam) {
        log.info("查询采集分页信息");
        EntityWrapper<YymCjdata> ew=new EntityWrapper<>();
        if(StringUtils.isNotEmpty(inputParam.getPname())){
            ew.like("pname_",inputParam.getPname());
        }
        if(StringUtils.isNotEmpty(inputParam.getCityname())){
            ew.like("cityname_",inputParam.getCityname());
        }
        Page<YymCjdata> result = yymCjdataService.selectPage(new Page(inputParam.getPage(),inputParam.getSize()),ew);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("rows", result.getRecords());
        map.put("total",result.getTotal());
        return map;
    }

    @RequestMapping(value = "save")
    public Map<String, Object> saveOrUpdate(String cjjson,String userid){
        log.info("保存采集");
        List<GdBean> cjlist=JSONArray.parseArray(cjjson,GdBean.class);
        //FixMe 批量插入比较好
        for (int i = 0; i < cjlist.size(); i++) {
            YymCjdata cjbean=new YymCjdata();
            cjbean.setName(cjlist.get(i).getName());
            cjbean.setCityname(cjlist.get(i).getCityname());
            cjbean.setAdname(cjlist.get(i).getAdname());
            cjbean.setPname(cjlist.get(i).getPname());
            cjbean.setMapId(cjlist.get(i).getId());
            cjbean.setAddress(cjlist.get(i).getAddress());
            cjbean.setLocation(cjlist.get(i).getLocation());
            cjbean.setTel(cjlist.get(i).getTel());
            cjbean.setBizType(cjlist.get(i).getBiz_type());
            Map rm=yymCjdataService.saveOrUpdate(cjbean);
            YymCjdata ryyb=(YymCjdata)rm.get("data");
            YymUserCjLink ucjl=new YymUserCjLink();
            ucjl.setCjId(ryyb.getId());
            ucjl.setUserId(userid);
            //如果有手机号码
            String[] phones=StringUtil.zNull(cjbean.getTel()).split(";");
            int j = 0;
            for (; j <phones.length ; j++) {
                if(StringUtil.isMobileNO(phones[j])){
                    break;
                }
            }
            if(j<phones.length){
                ucjl.setIsHandphone(1);
            }
            ucjl.setCreateTime(new Date());
            yymUserCjLinkService.saveOrUpdate(ucjl);
        }
        return Result.sucJsonResp(null);
    }

    @RequestMapping(value = "clearcj")
    public Map<String, Object> clearcj(String type,String userid) {
        if ("0".equals(type)) {
            log.info("清空采集");
            EntityWrapper<YymUserCjLink> ew = new EntityWrapper<>();
            ew.eq("user_id_", userid);
            ew.eq("is_txl_", 0);
            yymUserCjLinkService.delete(ew);
            return Result.sucJsonResp(null);
        } else if ("1".equals(type)) {
            log.info("清空通信录");
            EntityWrapper<YymUserCjLink> ew = new EntityWrapper<>();
            ew.eq("user_id_", userid);
            ew.eq("is_txl_", 1);
            yymUserCjLinkService.delete(ew);
            return Result.sucJsonResp(null);
        }
        return Result.failJsonResp("清空失败");
    }

    @RequestMapping(value = "exporttxl")
    public Map<String, Object> exporttxl(String userid,String ids) {
            log.info("导出通信录");
            EntityWrapper<YymUserCjLink> ew = new EntityWrapper<>();
            ew.eq("user_id_", userid);//"'"+ids+"'"
            ew.eq("is_txl_", 0);
            ew.in("cj_id_", ids);
            YymUserCjLink yuck=new YymUserCjLink();
            yuck.setIsTxl(1);
            boolean b=yymUserCjLinkService.update(yuck,ew);
            return Result.sucJsonResp(b);
    }

    @RequestMapping(value = "addtxl")
    public Map<String, Object> addtxl(String userid,String cjid) {
        log.info("导出通信录");
        EntityWrapper<YymUserCjLink> ew = new EntityWrapper<>();
        ew.eq("user_id_", userid);//"'"+ids+"'"
        ew.eq("is_txl_", 0);
        ew.eq("cj_id_", cjid);
        YymUserCjLink yuck=new YymUserCjLink();
        yuck.setIsTxl(1);
        boolean b=yymUserCjLinkService.update(yuck,ew);
        return Result.sucJsonResp(b);
    }

    @RequestMapping(value = "phonecall")
    public Map<String, Object> phonecall(String userid, String cjid) {
        log.info("打电话记录");
        EntityWrapper<YymUserCjLink> ew = new EntityWrapper<>();
        ew.eq("user_id_", userid);
        ew.in("cj_id_", cjid);
        YymUserCjLink yuck=new YymUserCjLink();
        yuck.setIsCall(1);
        boolean b=yymUserCjLinkService.update(yuck,ew);
        return Result.sucJsonResp(b);
    }
    @RequestMapping(value = "sendsms")
    public Map<String, Object> sendsms(String userid, String cjid) {
        log.info("发信息记录");
        EntityWrapper<YymUserCjLink> ew = new EntityWrapper<>();
        ew.eq("user_id_", userid);
        ew.in("cj_id_", cjid);
        YymUserCjLink yuck=new YymUserCjLink();
        yuck.setIsSms(1);
        boolean b=yymUserCjLinkService.update(yuck,ew);
        return Result.sucJsonResp(b);
    }

    @RequestMapping(value = "exportexcel")
    public void exportexcel(String ids,HttpServletResponse response) {
        log.info("导出excel文本");
        EntityWrapper<YymCjdata> ew=new EntityWrapper<>();
        ew.in("id_",ids);
        List<YymCjdata> list = yymCjdataService.selectList(ew);
        List<CjBeanPrint> plist=new ArrayList<>();
        for (YymCjdata item:list) {
            CjBeanPrint cbp=new CjBeanPrint();
            cbp.setAdname(item.getAdname());
            cbp.setCityname(item.getCityname());
            cbp.setName(item.getName());
            cbp.setPname(item.getPname());
            cbp.setTel(item.getTel());
            plist.add(cbp);
        }
        ExcelExport2.export(response,plist,new String[]{"地区名","城市名","名称","省名","电话"},"myexp");
    }

    @RequestMapping(value = "delete/{id}", method = RequestMethod.DELETE)
    public Map<String, Object> dele(@PathVariable("id") String id) {
        log.info("删除采集");
        EntityWrapper<YymUserCjLink> ew=new EntityWrapper<>();
        ew.eq("cj_id_",id);
        yymUserCjLinkService.delete(ew);
        yymCjdataService.deleteById(id);
        return Result.sucJsonResp(null);
    }
}

